import {
    drawCar, drawCarLocation, drawCharging,
    drawFour, drawImage,
    drawLine,
    drawLocation,
    drawRect,
    drawText,
    drawThreeBessel,
    drawTwoBessel,
    drawPoint,
    drawQuadratic,
    drawContainer
} from "@/draw/util/drawUtil"
import {getQuadratic,getArrow} from "../util/drawPathUtil";
export default function (G6){

    /**
     * 绘制线
     */
    G6.registerNode('line-node', {
        draw(cfg, group){
            return drawLine(cfg, group);
        },
        getAnchorPoints(self){
            const y1 = self.attrs.y1;
            const y2 = self.attrs.y2;
            return [
                [0,  y1 > y2 ? 1 : 0],
                [1,  y1 > y2 ? 0 : 1]
            ]
        },
    }, 'base-node');

    /**
     * 绘制文本
     */
    G6.registerNode('text-node', {
        draw(cfg, group){
            return drawText(cfg, group);
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [1, 1],
                [0, 1],
            ]
        },
    }, 'base-node');

    /**
     * 绘制二次贝尔曲线
     */
    G6.registerNode('twoBessel-node', {
        draw(cfg, group){
            return drawTwoBessel(cfg, group);
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [1, 1],
            ]
        },
    }, 'base-node');

    /**
     * 绘制三次贝塞尔曲线
     */
    G6.registerNode('threeBessel-node', {
        draw(cfg, group){
            return drawThreeBessel(cfg, group);
        },
        getAnchorPoints(self){
            return [
                [self.cfg.curve[0][1], self.cfg.curve[0][2]],
                [self.cfg.curve[1][1], self.cfg.curve[1][2]],
                [self.cfg.curve[1][3], self.cfg.curve[1][4]],
                [self.cfg.curve[1][5], self.cfg.curve[1][6]]
            ]
        },
    }, 'base-node')

    /**
     * 绘制四角
     */
    G6.registerNode('four-node', {
        draw(cfg, group){
            return drawFour(cfg, group);
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [1, 1],
                [0, 1]
            ]
        }
    }, 'base-node');

    /**
     * 绘制矩形
     */
    G6.registerNode('rect-node', {
        draw(cfg, group) {
           return drawRect(cfg, group);
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [1, 1],
                [0, 1]
            ]
        }
    }, 'base-node')

    /**
     * 绘制小车
     */
    G6.registerNode('car-node', {
        draw(cfg, group){
            return drawCar(cfg, group);
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [0, 1],
                [1, 1]
            ]
        }
    }, 'base-node');

    G6.registerNode('location-node', {
        draw(cfg, group){
            return drawLocation(cfg, group);
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [0, 1],
                [1, 1]
            ]
        }
    }, 'base-node');

    // 托盘
    G6.registerNode('container-node', {
        draw(cfg, group){
            return drawContainer(cfg, group);
        },
        afterDraw(cfg, group){
            const edge = group.get('children')[0];  // 获取边的绘制组
            edge.toFront();
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [0, 1],
                [1, 1]
            ]
        }
    }, 'base-node')

    /**
     * 绘制停车位
     */
    G6.registerNode('car-location-node', {
        draw(cfg, group){
            return drawCarLocation(cfg, group);
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [0, 1],
                [1, 1]
            ]
        }
    }, 'base-node');



    /**
     * 充电桩
     */
    G6.registerNode('charging-node', {
        draw(cfg, group){
           return drawCharging(cfg, group);
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [0, 1],
                [1, 1]
            ]
        }
    }, 'base-node');

    /**
     * 画背景图
     */
    G6.registerNode('map-node', {
        draw(cfg, group){
            const shope = drawImage(cfg, group);
            return shope;
        },
        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [0, 1],
                [1, 1]
            ]
        }
    }, 'base-node');

    /**
     * 画点
     */
    G6.registerNode('point-node', {
        draw(cfg, group){
            return drawPoint(cfg, group)
        }
    }, 'base-node')

    /**
     * 链接点和线
     */
    G6.registerNode('quadratic', {
        draw(cfg, group){
            return drawQuadratic(cfg, group);
        },
        update(cfg, node){
            node.getKeyShape().attr({
                path: getQuadratic(cfg.startPoint, cfg.endPoint, cfg.controlPoints),
                stroke: cfg.color ? cfg.color : '#000',
                lineWidth: cfg.lineWidth,
                endArrow: cfg.isArrow ? getArrow(cfg.color) : {path: []},
                lineDash: cfg.isDash ? [10, 5] : [0,0],
            });
        }
    })
}


